#include <stdio.h>
#define SIZE 7

void swap(int *a, int *b);
void bubble_sort(int arr[], int n);
void print_arr(int arr[], int n);

int main()
{
	int data[SIZE] = {2,3,43,9,10,27,38};
	int i,j;

	bubble_sort(data,SIZE);

	print_arr(data,SIZE);

	return 0;
}


void print_arr(int arr[],int n)
{
	printf("The array is: ");
	for(int i=0;i<n;i++)
	{
		printf("%d ", arr[i]);
	}
	printf("\n");
}

void swap(int *a, int *b)
{
	int temp;
	temp = *a;
	*a = *b;
	*b = temp;
}

void bubble_sort(int arr[], int n)
{
	int i,j;
	int sorted = 0;
	for (i = 0; i < n - 1; i++)
	{
			sorted = 0;
			for (j = 0; j < n - i - 1; j++)
			{		
				if (arr[j] > arr[j + 1])
				{
					swap(arr + j, arr + j + 1);
					sorted ++;
				}
			}

			printf("checked %d times: \n", i+1);

			if (sorted == 0)
			{
				printf("The array is already sorted\n");
				break;
			}
		}
}
